//
//  RZTipMessageCell.m
//  RZIM
//
//  Created by reyzhang on 2021/8/20.
//

#import "RZTipMessageCell.h"

@implementation RZTipMessageCell
#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self.baseContentView addSubview:self.tipMessageLabel];
    }
    return self;
}



- (void)setDataModel:(RZMessageInfo *)model {
    [super setDataModel:model];
    
    self.tipMessageLabel.text = model.msg; //默认赋值
    
    if (model.subType == MessageType_SYS_TIME) { //时间
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *date = [df dateFromString:model.msg];
        
        [df setDateFormat:@"MM-dd HH:mm"];
        NSString *dateStr = [df stringFromDate:date];
        
        self.tipMessageLabel.text = dateStr;
        
    }
    
    
    CGSize labelSize = [RZTipMessageCell getTipLabelSize:model];
    
    self.tipMessageLabel.textAlignment = NSTextAlignmentCenter;
    self.tipMessageLabel.frame = CGRectMake((self.baseContentView.bounds.size.width - labelSize.width) / 2.0f - 5, 0, labelSize.width + 10, labelSize.height);
    
    
    self.portraitImageView.hidden = YES;
    self.nicknameLabel.hidden = YES;
}


+ (CGSize)sizeForMessageModel:(RZMessageInfo *)model withCollectionViewWidth:(CGFloat)collectionViewWidth referenceExtraHeight:(CGFloat)extraHeight {
    CGFloat height = [self getTipLabelSize:model].height;
    height += extraHeight;
    return CGSizeMake(collectionViewWidth, height);
}


+ (CGSize)getTipLabelSize:(RZMessageInfo *)model{
    NSString *notification = model.msg;
    
    if (model.subType == MessageType_SYS_TIME) { //时间
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *date = [df dateFromString:model.msg];
        
        [df setDateFormat:@"MM-dd HH:mm"];
        NSString *dateStr = [df stringFromDate:date];
        
        notification = dateStr;
        
    }
    
    CGSize textSize = [RZKitUtility getTextDrawingSize:notification
                                                    font:[RZIMConfigCenter.font fontOfFourthLevel]
                                         constrainedSize:CGSizeMake([self getMaxLabelWidth], MAXFLOAT)];
    textSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    CGSize labelSize = CGSizeMake(textSize.width + 10, textSize.height + 6);
    return labelSize;
}

+ (CGFloat)getMaxLabelWidth{
    return SCREEN_WIDTH - 30 * 2;
}
#pragma mark - Getter
- (UILabel *)tipMessageLabel{
    if (!_tipMessageLabel) {
        _tipMessageLabel = [[UILabel alloc] init];
        _tipMessageLabel.backgroundColor = [UIColor colorWithRed:220/255.0 green:221/255.0 blue:224/255.0 alpha:1];
        _tipMessageLabel.textColor = [UIColor whiteColor];
        _tipMessageLabel.numberOfLines = 0;
        _tipMessageLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        _tipMessageLabel.textAlignment = NSTextAlignmentCenter;
        _tipMessageLabel.font = [RZIMConfigCenter.font fontOfFourthLevel];
        _tipMessageLabel.layer.masksToBounds = YES;
        _tipMessageLabel.layer.cornerRadius = 4.f;
    }
    return _tipMessageLabel;
}




@end
